package time;

import android.view.View;
import com.leji.song.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


public class WheelMain {

	private View view;
	private WheelView wv_year;
	private WheelView wv_month;
	private WheelView wv_day;
	private WheelView wv_hours;
	private WheelView wv_mins;
	public int screenheight;
	private static int START_YEAR = 1990, END_YEAR = 2100;

    ArrayList<String> dateList = new ArrayList<String>();
    ArrayList<String> timeList = new ArrayList<String>();

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public static int getSTART_YEAR() {
		return START_YEAR;
	}

	public static void setSTART_YEAR(int sTART_YEAR) {
		START_YEAR = sTART_YEAR;
	}

	public static int getEND_YEAR() {
		return END_YEAR;
	}

	public static void setEND_YEAR(int eND_YEAR) {
		END_YEAR = eND_YEAR;
	}

	public WheelMain(View view) {
		super();
		this.view = view;
		setView(view);
	}

    public void initDateTimePicker() {

        Calendar calendar = Calendar.getInstance();

        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
        String date1=df.format(calendar.getTime());
        dateList.add(date1);

        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String date2=df.format(calendar.getTime());
        dateList.add(date2);

        calendar.add(Calendar.DAY_OF_MONTH, 1);
        String date3=df.format(calendar.getTime());
        dateList.add(date3);


        // day
        wv_year = (WheelView) view.findViewById(R.id.day);
        wv_year.setAdapter(new StringWheelAdapter(dateList));// 设置"年"的显示数据
        wv_year.setCyclic(true);// 可循环滚动
        wv_year.setLabel("日");// 添加文字
        wv_year.setCurrentItem(0);// 初始化时显示的数据


        SimpleDateFormat df2=new SimpleDateFormat("HH:mm");

        String time1=df2.format(calendar.getTime());
        int nowTime = calendar.get(Calendar.MINUTE);

        if(nowTime < 30){
            calendar.set(Calendar.MINUTE, 30);
        }else{
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            calendar.set(Calendar.MINUTE, 0);
        }
        String time1_to=df2.format(calendar.getTime());

        timeList.add(time1 + "-" +  time1_to);


        //2
        if(calendar.get(Calendar.MINUTE) < 30){
            calendar.set(Calendar.MINUTE, 30);
        }else{
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            calendar.set(Calendar.MINUTE, 0);
        }
        String time2_to=df2.format(calendar.getTime());

        timeList.add(time1_to + "-" +  time2_to);

        //3
        if(calendar.get(Calendar.MINUTE) < 30){
            calendar.set(Calendar.MINUTE, 30);
        }else{
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            calendar.set(Calendar.MINUTE, 0);
        }
        String time3_to=df2.format(calendar.getTime());

        timeList.add(time2_to + "-" +  time3_to);

        //4
        if(calendar.get(Calendar.MINUTE) < 30){
            calendar.set(Calendar.MINUTE, 30);
        }else{
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            calendar.set(Calendar.MINUTE, 0);
        }
        String time4_to=df2.format(calendar.getTime());

        timeList.add(time3_to + "-" +  time4_to);

        //5
        if(calendar.get(Calendar.MINUTE) < 30){
            calendar.set(Calendar.MINUTE, 30);
        }else{
            calendar.add(Calendar.HOUR_OF_DAY, 1);
            calendar.set(Calendar.MINUTE, 0);
        }
        String time5_to=df2.format(calendar.getTime());

        timeList.add(time4_to + "-" +  time5_to);

        // time
        wv_month = (WheelView) view.findViewById(R.id.time);
        wv_month.setAdapter(new StringWheelAdapter(timeList));
        wv_month.setCyclic(true);
        wv_month.setLabel("分");
        wv_month.setCurrentItem(0);

        // 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
        int textSize = 0;
        textSize = (screenheight / 100) * 3;
        wv_month.TEXT_SIZE = textSize;
        wv_year.TEXT_SIZE = textSize;

    }
	/**
	 * @Description: TODO 弹出日期时间选择器
	 */
	public void initDateTimePicker(int year ,int month ,int day) {
//		int year = calendar.get(Calendar.YEAR);
//		int month = calendar.get(Calendar.MONTH);
//		int day = calendar.get(Calendar.DATE);

		// 添加大小月月份并将其转换为list,方便之后的判断
		String[] months_big = { "1", "3", "5", "7", "8", "10", "12" };
		String[] months_little = { "4", "6", "9", "11" };

		final List<String> list_big = Arrays.asList(months_big);
		final List<String> list_little = Arrays.asList(months_little);
        Calendar calendar = Calendar.getInstance();

        int year1 = calendar.get(Calendar.YEAR);
        int month1 = calendar.get(Calendar.MONTH);
        int day1 = calendar.get(Calendar.DAY_OF_MONTH);

        ArrayList<String> dateList = new ArrayList<String>();

        String date1 = Integer.toString(year1) + Integer.toString(month1) + Integer.toString(day1);

        dateList.add(date1);

        calendar.add(Calendar.DAY_OF_MONTH, 1);

        int year2 = calendar.get(Calendar.YEAR);
        int month2 = calendar.get(Calendar.MONTH);
        int day2 = calendar.get(Calendar.DAY_OF_MONTH);

        String date2 = Integer.toString(year2) + Integer.toString(month2) + Integer.toString(day2);
        dateList.add(date2);




		// 年
		wv_year = (WheelView) view.findViewById(R.id.day);
		wv_year.setAdapter(new StringWheelAdapter(dateList));// 设置"年"的显示数据
		wv_year.setCyclic(true);// 可循环滚动
		wv_year.setLabel("");// 添加文字
		wv_year.setCurrentItem(0);// 初始化时显示的数据

		// 月
		wv_month = (WheelView) view.findViewById(R.id.time);
		wv_month.setAdapter(new NumericWheelAdapter(1, 12));
		wv_month.setCyclic(true);
		wv_month.setLabel("月");
		wv_month.setCurrentItem(month);

		// 日
		wv_day = (WheelView) view.findViewById(R.id.day);
		wv_day.setCyclic(true);
		// 判断大小月及是否闰年,用来确定"日"的数据
		if (list_big.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 31));
		} else if (list_little.contains(String.valueOf(month + 1))) {
			wv_day.setAdapter(new NumericWheelAdapter(1, 30));
		} else {
			// 闰年
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
				wv_day.setAdapter(new NumericWheelAdapter(1, 29));
			else
				wv_day.setAdapter(new NumericWheelAdapter(1, 28));
		}
		wv_day.setLabel("日");
		wv_day.setCurrentItem(day - 1);

		// 添加"年"监听
		OnWheelChangedListener wheelListener_year = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int year_num = newValue + START_YEAR;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big
						.contains(String.valueOf(wv_month.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(wv_month
						.getCurrentItem() + 1))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if ((year_num % 4 == 0 && year_num % 100 != 0)
							|| year_num % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		// 添加"月"监听
		OnWheelChangedListener wheelListener_month = new OnWheelChangedListener() {
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				int month_num = newValue + 1;
				// 判断大小月及是否闰年,用来确定"日"的数据
				if (list_big.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 31));
				} else if (list_little.contains(String.valueOf(month_num))) {
					wv_day.setAdapter(new NumericWheelAdapter(1, 30));
				} else {
					if (((wv_year.getCurrentItem() + START_YEAR) % 4 == 0 && (wv_year
							.getCurrentItem() + START_YEAR) % 100 != 0)
							|| (wv_year.getCurrentItem() + START_YEAR) % 400 == 0)
						wv_day.setAdapter(new NumericWheelAdapter(1, 29));
					else
						wv_day.setAdapter(new NumericWheelAdapter(1, 28));
				}
			}
		};
		wv_year.addChangingListener(wheelListener_year);
		wv_month.addChangingListener(wheelListener_month);

		// 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
		int textSize = 0;
		textSize = (screenheight / 100) * 4;
		wv_day.TEXT_SIZE = textSize;
		wv_month.TEXT_SIZE = textSize;
		wv_year.TEXT_SIZE = textSize;

	}

	public String getTime() {
		StringBuffer sb = new StringBuffer();
		sb.append((wv_year.getCurrentItem() + START_YEAR)).append("-")
				.append((wv_month.getCurrentItem() + 1)).append("-")
				.append((wv_day.getCurrentItem() + 1));
		return sb.toString();
	}

    public String getTimeString() {
        StringBuffer sb = new StringBuffer();
        sb.append(dateList.get(wv_year.getCurrentItem())).append(" ")
                .append(timeList.get(wv_month.getCurrentItem()));
        return sb.toString();
    }
}
